import logging

from wine import settings
from wxchat.api.utils import wxClient, get_user_by_openid

logger = logging.getLogger("django")
# 模版ID FWhSIUDYXElMYj1S4fE-oObIbdI4DgDu3in3wAtFePo
# 开发者调用模版消息接口时需提供模版ID
# 标题 积分变更提醒
# 行业 消费品 - 消费品

# 详细内容
# {{first.DATA}}
# 卡号：{{keyword1.DATA}}
# 变更类型：{{keyword2.DATA}}
# 当前积分：{{keyword3.DATA}}
# {{remark.DATA}}


def send_message_to_retailer(bonus_point):
    """赠送积分通知"""
    template = 'FWhSIUDYXElMYj1S4fE-oObIbdI4DgDu3in3wAtFePo'  # 商家积分变更提醒

    color = "#173177"
    if bonus_point.unused_score > 0:
        remark = "本店{}赠送给客户可用积分{},其中待激活积分为{}.".format(bonus_point.name, bonus_point.score, bonus_point.unused_score)
    else:
        remark = "本店{}赠送给客户可用积分为{}".format(bonus_point.name, bonus_point.score)

    data = {
        'first': {
            "value": '您好，您的积分发生变更',
            "color": color,
        },
        "keyword1": {
           "value": bonus_point.retailer.name,
           "color": color,
        },
        "keyword2": {
           "value":  '赠送给客户{}积分'.format(bonus_point.score + bonus_point.unused_score),
           "color": color,
        },
        "keyword3": {
            "value": bonus_point.total_score,
            "color": color
        },
        "remark": {
           "value": remark,
           "color": "#f56c6c",
        }
    }
    logger.info(data)
    user = get_user_by_openid(bonus_point.openid)
    client = wxClient()
    if user.subscribe == 1:
        ret = client.message.send_template(user_id=bonus_point.openid, template_id=template, data=data, url=None)
        logger.info(ret)

    if bonus_point.retailer:
        openid = bonus_point.retailer.openid
        if bonus_point.openid != openid:
            retailer_user = get_user_by_openid(openid)
            if retailer_user.subscribe == 1:
                ret = client.message.send_template(user_id=openid, template_id=template, data=data, url=None)
                logger.info(ret)


def send_message_to_client(bonus_point):
    """赠送积分通知"""
    template = 'FWhSIUDYXElMYj1S4fE-oObIbdI4DgDu3in3wAtFePo'  # 客户积分变更提醒

    user = get_user_by_openid(bonus_point.customer_openid)

    if bonus_point.unused_score > 0:
        remark = "商家{}赠送您{}积分,其中可用积分{},待激活积分{}, 欢迎亲的再次光临，谢谢!".format(bonus_point.retailer.name, bonus_point.score + bonus_point.unused_score, bonus_point.score, bonus_point.unused_score),
    else:
        remark = "商家{}赠送您{}积分, 欢迎亲的再次光临，谢谢!".format(bonus_point.retailer.name, bonus_point.score)

    color = "#173177"
    data = {
        'first': {
            "value": '您好，您的积分发生变更',
            "color": color,
        },
        "keyword1": {
           "value": user.card_num,
           "color": color,
        },
        "keyword2": {
           "value":  "消费获赠{}积分".format(bonus_point.score + bonus_point.unused_score),
           "color": color,
        },
        "keyword3": {
            "value": "可用积分{},待激活积分{}".format(user.score, user.unused_score),
            "color": color
        },
        "remark": {
           "value": remark,
           "color": "#f56c6c",
        }
    }
    if user.subscribe == 1:
        client = wxClient()
        ret = client.message.send_template(user_id=user.openid, template_id=template, data=data, url=None)
        logger.info(ret)


# 模版ID so2jycZ5AE49fkTD1cqL8h4sWtLWg0nFp-FuQnM1Cvs
# 开发者调用模版消息接口时需提供模版ID
# 标题 积分消费通知
# 行业 消费品 - 消费品
# 详细内容
# {{first.DATA}}
# 消费店铺：{{keyword1.DATA}}
# 消费积分：{{keyword2.DATA}}
# 积分余额：{{keyword3.DATA}}
# 消费时间：{{keyword4.DATA}}
# {{remark.DATA}}

def send_consumption_message_to_client(point):
    """积分消费通知"""
    template = 'so2jycZ5AE49fkTD1cqL8h4sWtLWg0nFp-FuQnM1Cvs'  # 客户积分消费提醒

    user = get_user_by_openid(point.openid)
    employee = get_user_by_openid(point.employee_openid)

    color = "#173177"
    data = {
        'first': {
            "value": '您好，您的积分消费成功，明细如下:',
            "color": color,
        },
        "keyword1": {
           "value": settings.SHOP_NAME,
           "color": color,
        },
        "keyword2": {
           "value":  point.score,
           "color": color,
        },
        "keyword3": {
            "value": point.total_score,
            "color": color
        },
        "keyword4": {
            "value": point.update_time.strftime('%Y年%m月%d日 %H:%M'),
            "color": color
        },
        "remark": {
           "value": '期待您再次光临！',
           "color": "#f56c6c",
        }
    }

    client = wxClient()
    if user.subscribe == 1:
        client.message.send_template(user_id=user.openid, template_id=template, data=data, url=None)

    if employee.subscribe == 1:
        data["first"]["value"] = '您好,客户积分消费成功!'
        data["remark"]["value"] = point.name
        client.message.send_template(user_id=employee.openid, template_id=template, data=data, url=None)
